package com.yatu.codegen.execute;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.yatu.codegen.data.DataServiceImpl;
import com.yatu.codegen.util.ConvertUtil;
import com.yatu.codegen.util.FreeMakerUtil;
import com.yatu.codegen.util.PropertiesUtil;

public final class CodeMeachine {
	private static String generatePath = PropertiesUtil.load("generatePath", "D:\\generate\\");
	private static String packageName = PropertiesUtil.load("generatePath", "com.yatu");
	private static String serviceName = PropertiesUtil.load("service.name", "service");
	private static String repositoryName = PropertiesUtil.load("repository.name", "dao");
	private static String webName = PropertiesUtil.load("web.name", "controller");
	private static String entityName = PropertiesUtil.load("entity.name", "domain");
	private static String jspPrefix = PropertiesUtil.load("jsp.prefix", "\\jsp\\");
	private static String jspDir = PropertiesUtil.load("jsp.dir", "\\jsp");

	public static void setGeneratePath(String generatePath) {
		CodeMeachine.generatePath = generatePath;
	}

	public static void setPackageName(String packageName) {
		CodeMeachine.packageName = packageName;
	}

	public static void generateFile(String templateName, String tableName, String targetFileName) throws SQLException {
		// 验证代码生成目录
		if (generatePath != null && !generatePath.trim().equals("")) {
		} else {
			throw new IllegalArgumentException("不合法参数异常，文件名必须贷后最");
		}

		// 验证文件
		int index = targetFileName.lastIndexOf(".");
		if (index == -1) {
			throw new IllegalArgumentException("不合法参数异常，文件名必须带有后缀");
		}
		// 类名
		String className = ConvertUtil.format(ConvertUtil.convert(tableName));
		// 包名
		packageName = (packageName == null ? "" : packageName);

		// 生成目录(包含根目录)
		String outFilePath = generatePath;

		if (targetFileName.endsWith(".java")) {// 是否是java[根目录+报名全路径]
			String packageTemp = packageName;
			outFilePath += packageTemp.replace(".", "\\") + "\\";
		} else if (targetFileName.endsWith(".jsp")) {// 是否是jsp[根目录+模块]
			outFilePath += jspDir + "module\\".replace("module", ConvertUtil.convert(tableName));
		}

		// 目标文件没有文件名，默认是类名
		if (targetFileName.startsWith(".")) {
			targetFileName = className + targetFileName;
		}
		Map<String, Object> templateData = new HashMap<String, Object>();
		templateData.put("className", className);
		templateData.put("tableName", tableName);
		templateData.put("package", packageName);
		templateData.put("serviceName", serviceName);
		templateData.put("repositoryName", repositoryName);
		templateData.put("webName", webName);
		templateData.put("entityName", entityName);
		templateData.put("jspPrefix", jspPrefix);
		FreeMakerUtil.getInstance().generateFile(templateName,
				new DataServiceImpl().getDbTemplateData(tableName, templateData),
				getOutputFile(outFilePath, targetFileName));

	}

	private static String getOutputFile(String outFilePath, String targetFileName) {
		String temp = targetFileName;
		String module = entityName;
		if (temp.toUpperCase().contains(serviceName.toUpperCase())) {
			module = serviceName;
			if (temp.toUpperCase().contains(serviceName.toUpperCase() + "IMPL")) {
				module = serviceName + "\\" + "impl";
			}
		} else if (temp.toUpperCase().contains(repositoryName.toUpperCase())) {
			module = repositoryName;
			if (temp.toUpperCase().contains(repositoryName.toUpperCase() + "IMPL")) {
				module = repositoryName + "\\" + "impl";
			}
		} else if (temp.toUpperCase().contains(webName.toUpperCase())) {
			module = webName;

		} else if (temp.toUpperCase().contains(entityName.toUpperCase())) {
			module = entityName;
		} else if (temp.toUpperCase().endsWith(".JSP")) {
			module = "";
		}

		File folder = new File(outFilePath + module + "\\");
		if (!folder.exists() && !folder.isDirectory()) {
			folder.mkdirs();
		}

		return outFilePath + module + "\\" + targetFileName;
	}

	public static void setServiceName(String serviceName) {
		CodeMeachine.serviceName = serviceName;
	}

	public static String getRepositoryName() {
		return repositoryName;
	}

	public static void setRepositoryName(String repositoryName) {
		CodeMeachine.repositoryName = repositoryName;
	}

	public static String getWebName() {
		return webName;
	}

	public static void setWebName(String webName) {
		CodeMeachine.webName = webName;
	}

	public static String getEntityName() {
		return entityName;
	}

	public static void setEntityName(String entityName) {
		CodeMeachine.entityName = entityName;
	}

	public static String getJspPrefix() {
		return jspPrefix;
	}

	public static void setJspPrefix(String jspPrefix) {
		CodeMeachine.jspPrefix = jspPrefix;
	}

	public static String getServiceName() {
		return serviceName;
	}

}
